package com.cskaoyan.controller.stage;


import com.cskaoyan.model.Coupon;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.model.vo.ExceptionVO;
import com.cskaoyan.model.vo.WxCouponVO;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName: WxCouponController
 * @Description: TODO 微信优惠券模块
 * @Author:zhoukang
 * @Date: 2022/5/8 20:23
 * @Version: 1.0
 **/
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    @Autowired
    UserService userService;



    /**
     * @author: zk
     * @description: TODO 微信端查看优惠券列表
     * @date: 2022/5/9 8:59
     */
    @GetMapping("list")
    public BaseRespVO list(BaseParamBO param){
        DataVO<Coupon> couponDataVO = wxCouponService.selectCouponList(param);
        return BaseRespVO.ok(couponDataVO);
    }

    /**
     * @author: zk
     * @description: TODO 微信端用户查看已拥有优惠券列表
     * @date: 2022/5/9 15:04
     */
    @GetMapping("mylist")
    public BaseRespVO mylist(BaseParamBO param,Integer status){

        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = userService.getUserIdByUsername(username);
        //根据用户id与status查询优惠券列表
        DataVO<WxCouponVO> data = wxCouponService.selectMylistByStatus(param,status,userId);
        return BaseRespVO.ok(data);
    }

    /**
     * @author: zk
     * @description: TODO  领取优惠券
     * @date: 2022/5/9 17:14
     */
    @PostMapping("receive")
    public ExceptionVO receive(@RequestBody Map data){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        Coupon coupon = new Coupon();
        coupon.setId((Integer) data.get("couponId"));
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = userService.getUserIdByUsername(username);
        ExceptionVO exceptionVO = wxCouponService.receiveCoupon(userId,coupon);

        return exceptionVO;
    }

    /**
     * @author: zk
     * @description: TODO 兑换优惠券
     * @date: 2022/5/9 21:20
     */
    @PostMapping("exchange")
    public ExceptionVO exchange(@RequestBody Map data){
        Coupon coupon = new Coupon();
        coupon.setCode((String) data.get("code"));
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = userService.getUserIdByUsername(username);
        ExceptionVO exceptionVO = wxCouponService.exchangeCoupon(userId,coupon);
        return exceptionVO;
    }

    /**
     * @author: zk
     * @description: TODO 显示当前购物车可用优惠券列表
     * @date: 2022/5/9 22:32
     */
    @GetMapping("selectlist")
    public BaseRespVO selectList(Integer cartId,Integer grouponRulesId){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = userService.getUserIdByUsername(username);

        DataVO<WxCouponVO> data = wxCouponService.selectAvailableCouponList(userId,cartId);
        return BaseRespVO.ok(data);
    }
}
